﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class QuanLyChucVu : BasePage
    {
        ChucVuBLL CVBLL = new ChucVuBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            btnSua.Visible = false;
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaCV.Text != "" && txtTenCV.Text != "")
            {
                CHUCVU CV = new CHUCVU();
                CV.MaCV = int.Parse(txtMaCV.Text);
                CV.TenCV = txtTenCV.Text;
                CV.MoTa = txtMoTa.Text;

                if (CVBLL.KiemTraTonTai(CV.MaCV, CV.TenCV) != null)
                {
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", CVBLL.KiemTraTonTai(CV.MaCV, CV.TenCV) + " đã tồn tại!");
                }

                else
                {
                    CVBLL.ThemCV(CV);
                    dgvDSChucVu.DataBind();
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Thêm thành công!");

                }
            }
            else
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Vui lòng nhập đầy đủ thông tin chức vụ!");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            CHUCVU cv = new CHUCVU();
            cv.MaCV = int.Parse(txtMaCV.Text);
            cv.TenCV = txtTenCV.Text;
            cv.MoTa = txtMoTa.Text;

            CVBLL.SuaCV(cv);

            dgvDSChucVu.DataBind();
            Reset();
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Reset();
        }

        protected void Reset()
        {
            txtMaCV.Text = null;
            txtMaCV.Enabled = true;

            txtTenCV.Text = null;
            txtTenCV.Enabled = true;

            txtMoTa.Text = null;
            txtMoTa.Enabled = true;

            btnSua.Visible = false;

        }

        protected void ShowEditButton()
        {
            btnThem.Visible = false;
            btnSua.Visible = true;
        }

        protected void dgvDSChucVu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            ImageButton imgbtXoa = (ImageButton)e.Row.FindControl("imgbtXoa");
            ImageButton imgbtSua = (ImageButton)e.Row.FindControl("imgbtSua");
            ImageButton imgbtXem = (ImageButton)e.Row.FindControl("imgbtXem");

            if (imgbtXoa != null)
                imgbtXoa.Attributes["MaCV"] = e.Row.Cells[0].Text;
            if (imgbtSua != null)
                imgbtSua.Attributes["MaCV"] = e.Row.Cells[0].Text;
            if (imgbtXem != null)
                imgbtXem.Attributes["MaCV"] = e.Row.Cells[0].Text;
        }

        protected void imgbtXoa_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int macv = int.Parse(btn.Attributes["MaCV"]);

            try
            {
                CVBLL.XoaCV(macv);
                dgvDSChucVu.DataBind();
                Reset();
            }
            catch
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo", "Không thể xóa!");
            }
        }

        protected void imgbtSua_Click(object sender, ImageClickEventArgs e)
        {
            Reset();
            ImageButton btn = (ImageButton)sender;
            int macv = int.Parse(btn.Attributes["MaCV"]);

            if (IsPostBack)
            {
                CHUCVU CV = CVBLL.LayCV(macv);
                txtMaCV.Text = CV.MaCV.ToString();
                txtMaCV.Enabled = false;
                txtTenCV.Text = CV.TenCV;
                txtMoTa.Text = CV.MoTa;

                ShowEditButton();
            }
        }

        protected void imgbtXem_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton btn = (ImageButton)sender;
            int macv = int.Parse(btn.Attributes["MaCV"]);

            if (IsPostBack)
            {
                CHUCVU CV = CVBLL.LayCV(macv);

                txtMaCV.Text = CV.MaCV.ToString();
                txtMaCV.Enabled = false;

                txtTenCV.Text = CV.TenCV;
                txtTenCV.Enabled = false;

                txtMoTa.Text = CV.MoTa;
                txtMoTa.Enabled = false;

                btnThem.Visible = false;
            }
        }
    }
}